package com.example.jussi.myproject;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import java.util.ArrayList;
import java.util.List;

public class CardList extends AppCompatActivity {
    private List<CardBean> cardList = new ArrayList<>();
    MyDatabase  myDatabase;
    SQLiteDatabase database;
    CardBean cardBean;
    RecyclerView recyclerView;
    EditText companyName, name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_card_list);
        companyName = (EditText) findViewById(R.id.listCompanyName);
        name = (EditText) findViewById(R.id.listName);
        myDatabase = new MyDatabase(this);
        database = myDatabase.getReadableDatabase();
        initCard();
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        LinearLayoutManager layoutManager = new LinearLayoutManager(CardList.this);
        recyclerView.setLayoutManager(layoutManager);
        CardAdapter adapter = new CardAdapter(this, this, cardList);
        recyclerView.setAdapter(adapter);
    }

    public void quaryForCompanyName(View view){
        cardList.clear();
        recyclerView.removeAllViews();
        String sql = "select * from cards where business_name = ?";
        Cursor cursor = database.rawQuery(sql, new String[]{companyName.getText().toString()});
        while (cursor.moveToNext()){
            cardBean = new CardBean();
            cardBean.setId(cursor.getInt(cursor.getColumnIndex("id")));
            cardBean.setBusinessName(cursor.getString(cursor.getColumnIndex("business_name")));
            cardBean.setName(cursor.getString(cursor.getColumnIndex("name")));
            cardBean.setJob(cursor.getString(cursor.getColumnIndex("job")));
            cardBean.setEmail(cursor.getString(cursor.getColumnIndex("email")));
            cardBean.setPhoneNum(cursor.getString(cursor.getColumnIndex("phone")));
            cardBean.setQqNum(cursor.getString(cursor.getColumnIndex("qqnumber")));
            cardBean.setInfo(cursor.getString(cursor.getColumnIndex("info")));
            cardBean.setAddr(cursor.getString(cursor.getColumnIndex("address")));
            cardList.add(cardBean);
        }
        CardAdapter adapter = new CardAdapter(this, this, cardList);
        recyclerView.setAdapter(adapter);
    }

    public void quaryForName(View view){
        cardList.clear();
        recyclerView.removeAllViews();
        String sql = "select * from cards where name = ?";
        Cursor cursor = database.rawQuery(sql, new String[]{name.getText().toString()});
        while (cursor.moveToNext()){
            cardBean = new CardBean();
            cardBean.setId(cursor.getInt(cursor.getColumnIndex("id")));
            cardBean.setBusinessName(cursor.getString(cursor.getColumnIndex("business_name")));
            cardBean.setName(cursor.getString(cursor.getColumnIndex("name")));
            cardBean.setJob(cursor.getString(cursor.getColumnIndex("job")));
            cardBean.setEmail(cursor.getString(cursor.getColumnIndex("email")));
            cardBean.setPhoneNum(cursor.getString(cursor.getColumnIndex("phone")));
            cardBean.setQqNum(cursor.getString(cursor.getColumnIndex("qqnumber")));
            cardBean.setInfo(cursor.getString(cursor.getColumnIndex("info")));
            cardBean.setAddr(cursor.getString(cursor.getColumnIndex("address")));
            cardList.add(cardBean);
        }
        CardAdapter adapter = new CardAdapter(this, this, cardList);
        recyclerView.setAdapter(adapter);
    }

    private void initCard() {
        Intent data = getIntent();
        cardList = (List<CardBean>) data.getSerializableExtra("cardList");
    }
}
